package com.itheima.algorithm.binarytree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: TylerZhong
 * @description: 二叉树最大深度 - 使用层序遍历
 */
public class E06Leetcode111_1 {

    /**
     * 思路：
     *  1. 使用层序遍历，层数即最大深度
     * @param root
     * @return
     */
    public static int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int max = 0;
        while (!queue.isEmpty()) {
            max++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (poll.left == null && poll.right == null) {
                    return max;
                }
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
        }
        return max;
    }
}
